CCP=@CXX@
CCC=@CC@
PROTOC=@protoc@

PROG=_fann_train.bin
BUILD_DIR=@build_dir@

CFLAGS=@CFLAGS@ @includes@ -c 
LDFLAGS=@LIBS@

SRCS=$(wildcard *.cpp)
OBJS:=$(SRCS:%.cpp=$(BUILD_DIR)/%.o)

PROTO=$(wildcard *.proto)
PROTO_CC=$(PROTO:%.proto=%.pb.cc)
PROTO_OBJ=$(PROTO_CC:%.cc=./obj/%.o)

all:$(BUILD_DIR)/$(PROG) @install_targets@

$(BUILD_DIR)/$(PROG): dirs $(PROTO_CC) $(PROTO_OBJ) $(OBJS)
	@echo [LD] $(PROG); \
	$(CCP) $(OBJS) $(PROTO_OBJ) $(LDFLAGS) -o $(BUILD_DIR)/$(PROG)

install.gnulinux: FORCE
	@cp $(BUILD_DIR)/$(PROG) ../../bin/
	@cp ./bin/fann-train ../../bin/
	@echo Done
	
install.macos: FORCE
	@./inst_name_tool.sh $(BUILD_DIR)/$(PROG) libprotobuf.7.dylib  libs/libprotobuf.7.dylib
	@./inst_name_tool.sh $(BUILD_DIR)/$(PROG) libfloatfann.2.dylib libs/libfloatfann.2.dylib
	@./inst_name_tool.sh $(BUILD_DIR)/$(PROG) libpcre.0.dylib      libs/libpcre.0.dylib
	@cp $(BUILD_DIR)/$(PROG) ../../bin/
	@cp ./bin/fann-train ../../bin/
	@echo Done

dirs: FORCE
	-@if [ ! -d $(BUILD_DIR) ]; then mkdir $(BUILD_DIR); fi
	-@if [ ! -d ../../bin ]; then mkdir ../../bin; fi

$(BUILD_DIR)/%.o: %.cpp
	@echo [CC] $< ; \
	$(CCP) $(CFLAGS) $(CFLAGS_AUX) -MD -o $@ -c $< ;

$(BUILD_DIR)/%.o: %.cc
	@echo [CC] $< ; \
	$(CCP) $(CFLAGS) $(CFLAGS_AUX) -MD -o $@ -c $< ;
	
%.pb.cc: %.proto
	@echo [PROTO] $< ; \
	$(PROTOC) -I=./ --cpp_out=./ $< ;

include $(wildcard $(BUILD_DIR)/*.d) 

clean:
	rm -rf $(BUILD_DIR) *.pb.cc *.pb.h
	
FORCE:
